package com.atguigu.controller;


import com.atguigu.bean.Viewbean;
import com.atguigu.bean.Viewinfo;
import com.atguigu.bean.idBean;
import com.atguigu.bean.idName;
import com.atguigu.client.ProductFeignClient;
;
import com.atguigu.entity.SkuView;
import com.atguigu.result.RetVal;
import com.atguigu.service.SkuViewService;
import com.atguigu.util.DayUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangchaolove
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/view")
public class SkuViewController {
    @Autowired
    private SkuViewService skuViewService;

    @Resource
    private ProductFeignClient productFeignClient;

//    商品浏览数据量
    @PostMapping("/queryViewSku")
    public RetVal queryViewSku(@RequestBody Viewbean viewbean){
        List<idName> skuInfoForView = productFeignClient.getSkuInfoForView();

        Map<String ,Object> data = new HashMap<>();
        List<Viewinfo> viewinfoList = new ArrayList<>();
        //无条件全查
       if(viewbean.getId().size()==0 && viewbean.getBegin().length()==0){
           String start = DayUtils.getSdf().format(skuViewService.mostStartDate());
           String end = DayUtils.getSdf().format(new Date());
           List<String> dayList = DayUtils.getDays(start, end);
           data.put("xdata",dayList);
           //        2.获取所有类型
           List<idBean> types=skuViewService.types();
           for (idBean id: types) {
               Long productId = id.getProductId();

               List<Long> list1 = new LinkedList<>();
               for(String date: dayList){
                   Long viewnum =skuViewService.find(productId,date);
                   if(viewnum==null){
                       list1.add(0L);
                   }else{
                       list1.add(viewnum);
                   }
               }
               Viewinfo  viewinfo = new Viewinfo();
               viewinfo.setData(list1);
               viewinfo.setType("line");
               skuInfoForView.forEach(e->{
                   if(e.getId().equals(productId)){
                       viewinfo.setName(e.getName());
                   }
               });
//               data.put("ydata"+productId,viewinfo);
               viewinfoList.add(viewinfo);
           }
       }else if(viewbean.getId().size()==0 && viewbean.getBegin().length()>0){
           List<String> dayList = DayUtils.getDays(viewbean.getBegin(), viewbean.getEnd());
           data.put("xdata",dayList);
           //        2.获取所有类型
           List<idBean> types=skuViewService.types();
           for (idBean id: types) {
               Long productId = id.getProductId();
               List<Long> list1 = new LinkedList<>();
               for(String date: dayList){
                   Long viewnum =skuViewService.find(productId,date);
                   if(viewnum==null){
                       list1.add(0L);
                   }else{
                       list1.add(viewnum);
                   }
               }
               Viewinfo  viewinfo = new Viewinfo();
               viewinfo.setData(list1);
               viewinfo.setType("line");
               skuInfoForView.forEach(e->{
                   if(e.getId().equals(productId)){
                       viewinfo.setName(e.getName());
                   }
               });
//               data.put("ydata"+productId,viewinfo);
               viewinfoList.add(viewinfo);
           }
       }else if(viewbean.getId().size()>0 &&viewbean.getBegin().length()==0){
           String start = DayUtils.getSdf().format(skuViewService.mostStartDate());
           String end = DayUtils.getSdf().format(new Date());
           List<String> dayList = DayUtils.getDays(start, end);
           data.put("xdata",dayList);
           //        2.获取所有类型
//           List<idBean> types=skuViewService.types();
           List<Long> types=viewbean.getId();
           for (Long id: types) {
               Long productId = id;
               List<Long> list1 = new LinkedList<>();
               for(String date: dayList){
                   Long viewnum =skuViewService.find(productId,date);
                   if(viewnum==null){
                       list1.add(0L);
                   }else{
                       list1.add(viewnum);
                   }
               }
               Viewinfo  viewinfo = new Viewinfo();
               viewinfo.setData(list1);
               viewinfo.setType("line");
               skuInfoForView.forEach(e->{
                   if(e.getId().equals(productId)){
                       viewinfo.setName(e.getName());
                   }
               });
//               data.put("ydata"+productId,viewinfo);
               viewinfoList.add(viewinfo);
           }
       }else{
           List<String> dayList = DayUtils.getDays(viewbean.getBegin(), viewbean.getEnd());
           data.put("xdata",dayList);
           List<Long> types=viewbean.getId();
           for (Long id: types) {
               Long productId = id;
               List<Long> list1 = new LinkedList<>();
               for(String date: dayList){
                   Long viewnum =skuViewService.find(productId,date);
                   if(viewnum==null){
                       list1.add(0L);
                   }else{
                       list1.add(viewnum);
                   }
               }
               Viewinfo  viewinfo = new Viewinfo();
               viewinfo.setData(list1);
               viewinfo.setType("line");
               skuInfoForView.forEach(e->{
                   if(e.getId().equals(productId)){
                       viewinfo.setName(e.getName());
                   }
               });
//               data.put("ydata"+productId,viewinfo);
               viewinfoList.add(viewinfo);
           }
       }
        data.put("ydata",viewinfoList);
        return RetVal.ok(data);
    }

    @PostMapping("exportexcel")
    public List<SkuView> exportexcel(@RequestBody Viewbean viewbean){
        QueryWrapper<SkuView> wrapper = new QueryWrapper<>();
        if(!CollectionUtils.isEmpty(viewbean.getId())){
            wrapper.in("product_id",viewbean.getId());
        }
        if(!StringUtils.isEmpty(viewbean.getBegin())){
            wrapper.between("view_date",viewbean.getBegin(),viewbean.getEnd());
        }
        wrapper.orderByAsc("view_date");
        List<SkuView> list = skuViewService.list(wrapper);

        return list;
    }




}

